/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.client.widget.item.impl;

import com.genia.toolbox.web.gwt.basics.client.GwtHelper;
import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.form.client.form.SimpleItem;
import com.genia.toolbox.web.gwt.form.client.widget.item.ItemWidgetModifyListener;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;

/**
 * this class is the base SimpleItemWidget for all TextBoxBase Widget.
 * 
 * @param <ITEM>
 *          the type of item handled by this widget.
 */
public abstract class AbstractTextBoxBaseItemWidget<ITEM extends SimpleItem>
    extends AbstractSimpleValuedItemWidget<ITEM>
{
  /**
   * the TextBoxBase encapsulated in this SimpleItemWidget.
   */
  private final transient TextBoxBase textBoxBase;

  /**
   * the main panel of the <code>Composite</code>.
   */
  private final transient HorizontalPanel hPanel = new HorizontalPanel();

  /**
   * the list of all the <code>Widget</code> that must be modify to update the
   * error message.
   */
  private final transient Widget[] widgetsToToggle;



  /**
   * constructor.
   * 
   * @param simpleItem
   *          the item this object represents.
   */
  public AbstractTextBoxBaseItemWidget(final ITEM simpleItem)
  {
    super(simpleItem);
    textBoxBase = getTextBoxBase();
    widgetsToToggle = new Widget[] { textBoxBase, hPanel };
    // get the properties of a TextBoxItem.
    final GwtI18nMessage tBoxLabel = simpleItem.getLabelKey();
    final String tBoxName = simpleItem.getName();
    final boolean tBoxEnabled = simpleItem.isEnabled();

    final Label labelWidget = new Label();

    // set the text to the labelWidget.
    GwtHelper.fillText(labelWidget, tBoxLabel);

    textBoxBase.setName(tBoxName);
    textBoxBase.setEnabled(tBoxEnabled);

    // set the styleName.
    hPanel.addStyleName(getCssName());
    hPanel.addStyleName(simpleItem.getName());

    // add the widget to the panel.
    hPanel.add(labelWidget);
    hPanel.add(textBoxBase);

    initWidget(hPanel);

  }



  /**
   * add a keyboard listener to the underlying TextBox.
   * 
   * @param keyboardListener
   *          the keyboard listener to add
   */
  public void addKeyboardListener(final KeyboardListener keyboardListener)
  {
    textBoxBase.addKeyboardListener(keyboardListener);
  }



  /**
   * returns the value of the texBox.
   * 
   * @return the value of a textBox.
   */
  @Override
  public String getValue()
  {
    return textBoxBase.getText();
  }



  /**
   * add a listener that is called each time the value of this object change.
   * 
   * @param modifyListener
   *          the listener.
   */
  public void addModifyListener(final ItemWidgetModifyListener modifyListener)
  {
    textBoxBase.addChangeListener(new ChangeListener()
    {

      public void onChange(final Widget widget)
      {
        modifyListener.onModify(AbstractTextBoxBaseItemWidget.this);
      }

    });

    textBoxBase.addKeyboardListener(new KeyboardListenerAdapter()
    {

      @Override
      public void onKeyUp(final Widget widget, final char key, final int modifier)
      {
        modifyListener.onModify(AbstractTextBoxBaseItemWidget.this);
      }

    });

  }



  /**
   * set the value of the textBox.
   * 
   * @param value
   *          the values to set.
   */
  @Override
  public void setValue(final String value)
  {
    textBoxBase.setText(value);
  }



  /**
   * returns the CSS name of this widget.
   * 
   * @return the CSS name of this widget
   */
  protected abstract String getCssName();



  /**
   * returns the TextBoxBase encapsulated in this SimpleItemWidget.
   * 
   * @return the TextBoxBase encapsulated in this SimpleItemWidget
   */
  protected abstract TextBoxBase getTextBoxBase();



  /**
   * returns an array containing all the widget that the setErrorMessage must
   * modify.
   * 
   * @return an array containing all the widget that the setErrorMessage must
   *         modify
   */
  @Override
  protected Widget[] getWidgetsToToggle()
  {
    return widgetsToToggle;
  }
}
